package com.shizhanzhe.zxbapp.task;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.customtask.ScreenTaskDetailsActivity;
import com.shizhanzhe.zxbapp.pay.PayVIPActivity;
import com.shizhanzhe.zxbapp.ui.LoginActivity;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.widget.NoScrollWebView;
import com.shizhanzhe.zxbapp.widget.VipDIalog;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by zz9527 on 2018/10/19.
 */
public class WebActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        ImmersionBar.with(this).statusBarColor(R.color.them).fitsSystemWindows(true).init();
        findViewById(R.id.backarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        TextView title = findViewById(R.id.web_title);
        WebView web = findViewById(R.id.web);
        final ProgressBar pg1 = findViewById(R.id.progressBar);
        web.setBackgroundColor(0);
        web.getSettings().setJavaScriptEnabled(true);

        web.setVerticalScrollBarEnabled(false);
        web.setHorizontalScrollBarEnabled(false);
        web.getSettings().setUseWideViewPort(true);//设置webview显示屏幕宽度 不能滑动
        web.getSettings().setLoadWithOverviewMode(true);
        web.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView webView, int newProgress) {
                if (newProgress == 100) {
                    pg1.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    pg1.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    pg1.setProgress(newProgress);//设置进度值
                }
            }
        });
        web.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

        LinearLayout share=findViewById(R.id.share);
        LinearLayout sharewx = findViewById(R.id.share_wx);
        LinearLayout sharepyq = findViewById(R.id.share_pyq);
        TextView fen = findViewById(R.id.fen);
        int isshow = getIntent().getIntExtra("isshow", 0);
        if (!"".equals(getIntent().getStringExtra("fen"))) {
            if (isshow==0){
                web.getSettings().setTextSize(WebSettings.TextSize.LARGEST);
                web.loadDataWithBaseURL(null, getIntent().getStringExtra("scon"), "text/html", "utf-8", null);
            }else {
                web.loadUrl(getIntent().getStringExtra("url"));
            }
            fen.setText(Html.fromHtml("已获得<font color='#ff0000'>" + getIntent().getStringExtra("fen") + "元</font>"));
        }else {
            title.setText("信用卡任务");
            share.setVisibility(View.GONE);
            web.loadUrl(getIntent().getStringExtra("url"));
        }
        sharewx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid == 0) {
                    startActivity(new Intent(WebActivity.this, LoginActivity.class));
                } else {
                    Wechat.ShareParams sp = new Wechat.ShareParams();
                    sp.setTitle(getIntent().getStringExtra("title"));
                    sp.setText(getIntent().getStringExtra("intro"));
                    sp.setUrl(Path.SHAREURL(getIntent().getStringExtra("ym"),UserBean.uid, getIntent().getIntExtra("shid", 0)));
                    Log.i("__________",Path.SHAREURL(getIntent().getStringExtra("ym"),UserBean.uid, getIntent().getIntExtra("shid", 0)));
                    sp.setImageUrl(Path.img(getIntent().getStringExtra("img")));
                    sp.setShareType(Platform.SHARE_WEBPAGE);
                    Platform weChat = ShareSDK.getPlatform(Wechat.NAME);
//                    weChat.setPlatformActionListener(new PlatformActionListener() {
//                        @Override
//                        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
//                            //分享成功的回调
//                            Toast.makeText(ShareWebActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
//                        }
//
//                        @Override
//                        public void onError(Platform platform, int i, Throwable throwable) {
//                            Toast.makeText(ShareWebActivity.this, "分享异常", Toast.LENGTH_SHORT).show();
//                        }
//
//                        @Override
//                        public void onCancel(Platform platform, int i) {
//                            Toast.makeText(ShareWebActivity.this, "分享取消", Toast.LENGTH_SHORT).show();
//                        }
//                    });
                    if (getIntent().getIntExtra("type", 0) == 1) {
                        if (UserBean.level > 0) {
                            weChat.share(sp);
                            OkHttpDownloadJsonUtil.downloadJson(WebActivity.this, Path.SHARECALLBACK(UserBean.uid, getIntent().getIntExtra("shid", 0)), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                                @Override
                                public void onsendJson(String json) {

                                }
                            });
                        } else {
                            final VipDIalog vipDIalog = new VipDIalog(WebActivity.this);
                            vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                                @Override
                                public void chooseTrue() {
                                    if (UserBean.gao > 0) {
                                        startActivity(new Intent(WebActivity.this, PayVIPActivity.class));
                                    } else {
                                        Toast.makeText(WebActivity.this, "首次提现方能开通", Toast.LENGTH_SHORT).show();
                                    }
                                    vipDIalog.dismiss();
                                }

                                @Override
                                public void chooseFalse() {
                                    vipDIalog.dismiss();

                                }
                            });
                            vipDIalog.show();
                            vipDIalog.setContent();
                        }
                    } else {
                        weChat.share(sp);
                        OkHttpDownloadJsonUtil.downloadJson(WebActivity.this, Path.SHARECALLBACK(UserBean.uid, getIntent().getIntExtra("shid", 0)), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                            @Override
                            public void onsendJson(String json) {

                            }
                        });
                    }

                }
            }
        });
        sharepyq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid == 0) {
                    startActivity(new Intent(WebActivity.this, LoginActivity.class));
                } else {
                    WechatMoments.ShareParams sp = new WechatMoments.ShareParams();
                    sp.setTitle(getIntent().getStringExtra("title"));
                    sp.setText("赚小宝平台，一个用手机赚钱的App");
                    sp.setUrl(Path.SHAREURL(getIntent().getStringExtra("ym"),UserBean.uid, getIntent().getIntExtra("shid", 0)));
                    sp.setShareType(Platform.SHARE_WEBPAGE);
                    Platform weChatMoments = ShareSDK.getPlatform(WechatMoments.NAME);
//                    weChat.setPlatformActionListener(new PlatformActionListener() {
//                        @Override
//                        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
//                            //分享成功的回调
//                            Toast.makeText(ShareWebActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
//                        }
//
//                        @Override
//                        public void onError(Platform platform, int i, Throwable throwable) {
//                            Toast.makeText(ShareWebActivity.this, "分享异常", Toast.LENGTH_SHORT).show();
//                        }
//
//                        @Override
//                        public void onCancel(Platform platform, int i) {
//                            Toast.makeText(ShareWebActivity.this, "分享取消", Toast.LENGTH_SHORT).show();
//                        }
//                    });
                    if (getIntent().getIntExtra("type", 0) == 1) {
                        if (UserBean.level > 0) {
                            weChatMoments.share(sp);
                            OkHttpDownloadJsonUtil.downloadJson(WebActivity.this, Path.SHARECALLBACK(UserBean.uid, getIntent().getIntExtra("shid", 0)), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                                @Override
                                public void onsendJson(String json) {

                                }
                            });
                        } else {
                            final VipDIalog vipDIalog = new VipDIalog(WebActivity.this);
                            vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                                @Override
                                public void chooseTrue() {
                                    if (UserBean.gao > 0) {
                                        startActivity(new Intent(WebActivity.this, PayVIPActivity.class));
                                    } else {
                                        Toast.makeText(WebActivity.this, "首次提现方能开通", Toast.LENGTH_SHORT).show();
                                    }
                                    vipDIalog.dismiss();
                                }

                                @Override
                                public void chooseFalse() {
                                    vipDIalog.dismiss();

                                }
                            });
                            vipDIalog.show();
                            vipDIalog.setContent();
                        }
                    } else {
                        weChatMoments.share(sp);
                        OkHttpDownloadJsonUtil.downloadJson(WebActivity.this, Path.SHARECALLBACK(UserBean.uid, getIntent().getIntExtra("shid", 0)), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                            @Override
                            public void onsendJson(String json) {

                            }
                        });
                    }

                }
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy(); //必须调用该方法，防止内存泄漏
    }

    private void showShare(String platform) {
        final OnekeyShare oks = new OnekeyShare();
        //指定分享的平台，如果为空，还是会调用九宫格的平台列表界面
        if (platform != null) {
            oks.setPlatform(platform);
        }
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(getIntent().getStringExtra("title"));
//        // titleUrl是标题的网络链接，仅在Linked-in,QQ和QQ空间使用
//        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("赚小宝平台,一个能赚钱的手机App");
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl(Path.img(getIntent().getStringExtra("img")));
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(getIntent().getStringExtra("url"));
//        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
//        oks.setComment("我是测试评论文本");
//        // site是分享此内容的网站名称，仅在QQ空间使用
//        oks.setSite("ShareSDK");
//        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
//        oks.setSiteUrl("http://sharesdk.cn");
        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                //失败的回调，arg:平台对象，arg1:表示当前的动作，arg2:异常信息
                Toast.makeText(WebActivity.this, "分享异常", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap arg2) {
                //分享成功的回调
                Toast.makeText(WebActivity.this, "分享成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
                //取消分享的回调
                Toast.makeText(WebActivity.this, "分享取消", Toast.LENGTH_SHORT).show();
            }
        });
        //启动分享
        oks.show(this);
    }
}
